package com.bjtech.web.action.management;

import java.util.HashMap;
import java.util.Map;

import com.bjtech.dao.AssignMeterInfoDao;
import com.bjtech.dao.BaseBuildingInfoDao;
import com.bjtech.dao.BaseRoomInfoDao;
import com.bjtech.dao.BaseUnitInfoDao;
import com.bjtech.dao.SinkInfoDao;
import com.bjtech.domain.AssignMeterInfo;
import com.bjtech.domain.BaseBuildingInfo;
import com.bjtech.domain.BaseRoomInfo;
import com.bjtech.domain.BaseUnitInfo;
import com.bjtech.domain.SinkInfo;
import com.bjtech.domain.query.AssignMeterInfoQuerySpec;
import com.bjtech.web.action.LyheatBaseAction;

public class AssignMeterInfoAction extends LyheatBaseAction {

	private static final long serialVersionUID = 1L;
	private AssignMeterInfoDao assignMeterInfoDao;
	private AssignMeterInfoQuerySpec selectBean;
	
	private Long room_id;
	private BaseRoomInfoDao baseRoomInfoDao;
	private BaseUnitInfoDao baseUnitInfoDao;
	private BaseBuildingInfoDao baseBuildingInfoDao;
	private BaseRoomInfo roomInfo;
	private BaseUnitInfo unitInfo;
	private BaseBuildingInfo buildingInfo;
	
	private SinkInfoDao sinkInfoDao;
	
	
	private String custom;
	
	private Map tjParam = new HashMap();
	
	public void prepare() throws Exception {

	}
	
	public AssignMeterInfoAction() throws Exception{
		model = new AssignMeterInfo();
		selectBean = new AssignMeterInfoQuerySpec();
	}
	
	public void setAssignMeterInfoDao(AssignMeterInfoDao assignMeterInfoDao) {
		this.assignMeterInfoDao = assignMeterInfoDao;
	}

	public String execute(){
		return this.list();
	}
	
	public String list(){
		selectBean.setRoom_id(room_id);
		selectBean.queryParamString();
		list = this.assignMeterInfoDao.selectEntityPaginatedList(new AssignMeterInfo(),selectBean);
		
		//查询附属的户主信息
		roomInfo = this.baseRoomInfoDao.selectEntity(new BaseRoomInfo(), room_id);
		unitInfo = this.baseUnitInfoDao.selectEntity(new BaseUnitInfo(), roomInfo.getUnit_no());
		buildingInfo = this.baseBuildingInfoDao.selectEntity(new BaseBuildingInfo(), roomInfo.getBuild_id());
		return "list";
	}
	
	public String add(){
		model = new AssignMeterInfo();
		((AssignMeterInfo)model).setRoom_id(room_id);
		return "edit";
	}
	
	public String save(){
		AssignMeterInfo entity = (AssignMeterInfo)model;
		BaseRoomInfo room = baseRoomInfoDao.selectEntity(new BaseRoomInfo(), entity.getRoom_id());
		entity.setBuild_id(room.getBuild_id());
		BaseBuildingInfo build = baseBuildingInfoDao.selectEntity(new BaseBuildingInfo(), room.getBuild_id());
		entity.setArea_id(build.getArea_id());
		
		if(entity.getId()==null){
			assignMeterInfoDao.insertEntity(entity);
		}else{
			assignMeterInfoDao.updateEntity(entity);
		}
		room_id = entity.getRoom_id();
		//custom = "AssignMeterInfo!list.action?room_id="+entity.getRoom_id();
		return this.list();
	}
	
	public String edit(){
		model = this.assignMeterInfoDao.selectEntity(new AssignMeterInfo(),id);
		return "edit";
	}

	public String view(){
		model = this.assignMeterInfoDao.selectEntity(new AssignMeterInfo(),id);
		tjParam = this.assignMeterInfoDao.selectTjParamForAssignMeterInfo(id);
		return "view";
	}
	
	public String delete(){
		AssignMeterInfo temp = this.assignMeterInfoDao.selectEntity(new AssignMeterInfo(),id);
		model = this.assignMeterInfoDao.deleteEntity(new AssignMeterInfo(),id);
		this.sinkInfoDao.deleteEntity(new SinkInfo(), temp.getSink_id());
		room_id = temp.getRoom_id();
		return this.list();
	}
	
	public AssignMeterInfoQuerySpec getSelectBean() {
		return selectBean;
	}

	public void setSelectBean(AssignMeterInfoQuerySpec selectBean) {
		this.selectBean = selectBean;
	}

	public Long getRoom_id() {
		return room_id;
	}

	public void setRoom_id(Long roomId) {
		room_id = roomId;
	}

	public AssignMeterInfoDao getAssignMeterInfoDao() {
		return assignMeterInfoDao;
	}

	public BaseRoomInfoDao getBaseRoomInfoDao() {
		return baseRoomInfoDao;
	}

	public void setBaseRoomInfoDao(BaseRoomInfoDao baseRoomInfoDao) {
		this.baseRoomInfoDao = baseRoomInfoDao;
	}

	public BaseRoomInfo getRoomInfo() {
		return roomInfo;
	}

	public void setRoomInfo(BaseRoomInfo roomInfo) {
		this.roomInfo = roomInfo;
	}

	public BaseUnitInfo getUnitInfo() {
		return unitInfo;
	}

	public void setUnitInfo(BaseUnitInfo unitInfo) {
		this.unitInfo = unitInfo;
	}

	public BaseBuildingInfo getBuildingInfo() {
		return buildingInfo;
	}

	public void setBuildingInfo(BaseBuildingInfo buildingInfo) {
		this.buildingInfo = buildingInfo;
	}

	public BaseUnitInfoDao getBaseUnitInfoDao() {
		return baseUnitInfoDao;
	}

	public void setBaseUnitInfoDao(BaseUnitInfoDao baseUnitInfoDao) {
		this.baseUnitInfoDao = baseUnitInfoDao;
	}

	public String getCustom() {
		return custom;
	}

	public void setCustom(String custom) {
		this.custom = custom;
	}

	public BaseBuildingInfoDao getBaseBuildingInfoDao() {
		return baseBuildingInfoDao;
	}

	public void setBaseBuildingInfoDao(BaseBuildingInfoDao baseBuildingInfoDao) {
		this.baseBuildingInfoDao = baseBuildingInfoDao;
	}

	public SinkInfoDao getSinkInfoDao() {
		return sinkInfoDao;
	}

	public void setSinkInfoDao(SinkInfoDao sinkInfoDao) {
		this.sinkInfoDao = sinkInfoDao;
	}

	public Map getTjParam() {
		return tjParam;
	}

	public void setTjParam(Map tjParam) {
		this.tjParam = tjParam;
	}
}
